package com.yami.shop.mystery.multishop.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.mystery.common.dto.ActivityRuleOptions;
import com.yami.shop.mystery.common.model.ActivityRule;
import com.yami.shop.mystery.common.service.ActivityRuleService;
import com.yami.shop.mystery.common.vo.ActivityRulePageVo;
import com.yami.shop.security.common.util.AuthUserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 活动规则
 * @author wxf
 * @date 2023/4/17 9:49
 */
@RestController
@RequestMapping("/activity/activityRule")
@Tag(name="活动规则接口")
public class ActivityRuleController {

    @Autowired
    private ActivityRuleService activityRuleService;

    @GetMapping("/page")
    @Operation(summary = "活动规则分页列表")
    public ServerResponseEntity<IPage<ActivityRule>> pageList(PageParam<ActivityRule> page, @ParameterObject ActivityRulePageVo vo) {

        vo.setShopId(AuthUserContext.get().getShopId());
        IPage<ActivityRule> pageRule = activityRuleService.pageList(page, vo);
        return ServerResponseEntity.success(pageRule);
    }

    @PostMapping("/save")
    @Operation(summary = "新增/修改活动规则")
    public ServerResponseEntity<Void> saveOrUpdate(@RequestBody ActivityRule dto) {

        if (Objects.isNull(dto.getRuleId()) || dto.getRuleId() < 1) {
            dto.setStatus(1);
        }
        activityRuleService.saveOrUpdate(dto);
        return ServerResponseEntity.success();
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除活动规则")
    @Parameter(name = "id", description = "活动规则id", required = true)
    public ServerResponseEntity<Void> deleteBrand(@PathVariable Long id) {
        activityRuleService.removeIds(Lists.newArrayList(id));
        return ServerResponseEntity.success();
    }

    @GetMapping("/info/{ruleId}")
    @Operation(summary = "获取活动规则详细信息")
    @Parameter(name = "ruleId", description = "活动规则主键", required = true)
    public ServerResponseEntity<ActivityRule> info(@PathVariable("ruleId") Long ruleId) {
        ActivityRule coupon = activityRuleService.detail(ruleId);
        return ServerResponseEntity.success(coupon);
    }

    @GetMapping("/updateStatus")
    public ServerResponseEntity<Boolean> updateStatus(@RequestParam("ruleId") String ruleId, @RequestParam("status") String status){
        Boolean falge = activityRuleService.updateStatus(ruleId,status);
        return ServerResponseEntity.success(falge);
    }

    @GetMapping("/getRuleTypeOptions")
    @Operation(summary = "规则类型Options")
    public ServerResponseEntity<List<ActivityRuleOptions>> getRuleTypeOptions(){
        List<ActivityRuleOptions> list = activityRuleService.getRuleTypeOptions();
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/getConditionTypeOptions")
    @Operation(summary = "活动条件类型Options")
    public ServerResponseEntity<List<ActivityRuleOptions>> getConditionTypeOptions(){
        List<ActivityRuleOptions> list = activityRuleService.getConditionTypeOptions();
        return ServerResponseEntity.success(list);
    }

    @GetMapping("/list")
    @Operation(summary = "活动规则分页列表")
    public ServerResponseEntity<List<ActivityRule>> getList(@ParameterObject ActivityRulePageVo vo) {
        vo.setShopId(AuthUserContext.get().getShopId());
        List<ActivityRule> pageRule = activityRuleService.getList(vo);
        return ServerResponseEntity.success(pageRule);
    }
}
